/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managerPeer;

import common.function_common;
import common.global;
import java.io.*;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JList;

/**
 *
 * @author DongVan
 */
public class NodeMap {
    
    private JList m_ListGUI;
    private ArrayList<Node> m_ListNode;
    private DefaultListModel listModel;

    public NodeMap(JList listGUI){
        this.m_ListGUI = listGUI;
    }
    
    public ArrayList<Node> getM_ListNode() {
        return m_ListNode;
    }

    public void setM_ListNode(ArrayList<Node> m_ListNode) {
        this.m_ListNode = m_ListNode;
    }
    
    public void readFilePeers(){
        if(this.m_ListNode == null){
            this.m_ListNode = new ArrayList <Node>();
        }else{
            this.m_ListNode.clear();
        }
        try{
            BufferedReader input = new BufferedReader(new FileReader("src\\FolderListPeer\\nodeMap.txt"));
            String line="";
            line = input.readLine();
            int numOfClient = Integer.valueOf(line);
            while ((line = input.readLine()) != null) {
                String [] str = line.split(";");
                int id = Integer.parseInt(str[0]);
                String ipAddress = str[1];
                int port = Integer.parseInt(str[2]);
                int cost = Integer.parseInt(str[3]);
                this.m_ListNode.add(new Node(id,ipAddress,port,cost));
            }
            input.close();
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
    
    public int getCostByIpAddress(String ipAddress){
        for(Node node : this.m_ListNode){
            if(node.getM_IdAddress().equals(ipAddress)){
                return node.getM_cost();
            }    
        }
        return 0;
    }
    
    public void updateFilePeers(Node peer){
        if(!global.nodes.m_ListNode.contains(peer)){
           try{
           BufferedWriter output = new BufferedWriter(new FileWriter("nodeMap.txt"));
           String line = "";
           for(int i = 0; i < this.m_ListNode.size(); i++){
               Node node = new Node();
               node = this.m_ListNode.get(i);
               line = node.getM_ID() + node.getM_IdAddress() + node.getM_Port();
               output.newLine();
               output.write(line);
           }
           output.close();
        }
            catch(Exception ex){
                ex.printStackTrace();
            }
           this.updateListPeerOntoGUI();
        }
        
    }

    public void updateListPeerOntoGUI(){
        this.readFilePeers();
        if(getListModel() == null){
            setListModel(new DefaultListModel());
            this.m_ListGUI.setModel(getListModel());
        }else{
            getListModel().clear();
        }
        for(Node item : this.getM_ListNode()){
            getListModel().addElement(item.pcInformation()); 
        }
    }
    
    /**
     * @return the m_ListGUI
     */
    public JList getM_ListGUI() {
        return m_ListGUI;
    }

    /**
     * @param m_ListGUI the m_ListGUI to set
     */
    public void setM_ListGUI(JList m_ListGUI) {
        this.m_ListGUI = m_ListGUI;
    }

    /**
     * @return the listModel
     */
    public DefaultListModel getListModel() {
        return listModel;
    }

    /**
     * @param listModel the listModel to set
     */
    public void setListModel(DefaultListModel listModel) {
        this.listModel = listModel;
    }
    
}
